<?php

/**
 * Class ApiUser
 */
class ApiUser extends ApiBase
{
    /**
     * //okokokok
     * @param array $par , urhfhx
     * @param array $params , urhfhx
     * @return array,u返回数组, jgyugy
     */

    public static function get($params = array())
    {
        return parent::get_models($params, User::model());
    }

    public static function is_api_auth($params = array())
    {
        $username = '';
        if (empty($params['class'])) {
            return array('result' => 'false', 'msg' => 'Class name is required');
        }
        if (empty($params['method'])) {
            return array('result' => 'false', 'msg' => 'Method name is required');
        }
        $user = null;
        if (!empty($params['token'])) {
            $user = User::model()->findByAttributes(array('token' => $params['token']));
        } elseif (!empty($params['user'])) {
            $user = User::model()->findByAttributes(array('name' => $params['user']));
            $pwd = empty($params['password']) ? '' : $params['password'];
            if (!$user->validatePassword($pwd)) {
                return array('result' => 'false', 'msg' => 'Authentication Failure');
            }
        }

        //check auth_api,now is none,just check isAdmin or not
        if ($user->isAdmin()) {
            return array('result' => 'true');
        } else {
            return array('result' => 'false', 'msg' => 'Authentication Failed');
        }
        //
    }


    //获取用户所有的认证
    /**
     * @param string $params , sfsih
     * @return mixed
     */
    public static function get_auths($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $auths = User::model()->getAuths($userid);
        return $auths;
    }

    //获取用户所有的子用户
    /**
     * @param $params
     * @return array
     */
    public static function get_subusers($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $users = User::model()->getSubUsers($userid);
        $arUsers = dotm_ar_models2ars($users);
        return $arUsers;
    }

    /**
     * @param $params
     */
    public static function set_config($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $module = isset($params['module']) ? $params['module'] : '';
        $type = isset($params['type']) ? $params['type'] : '';
        $configs = isset($params['config']) ? $params['config'] : '';

        UserConfig::setConfig($userid, $module, $type, $configs);
    }

    /**
     * @param $params
     * @return mixed|null|string
     */
    public static function get_config($params)
    {
        $userid = isset($params['userid']) ? $params['userid'] : 0;
        $module = isset($params['module']) ? $params['module'] : '';
        $type = isset($params['type']) ? $params['type'] : '';

        return UserConfig::getConfig($userid, $module, $type);
    }


}